package com.testing.month_exam_a;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.List;

/**
 * Created by Administrator on 2017/3/1.
 */

public class RecyclerViewAdapter2 extends RecyclerView.Adapter<RecyclerViewAdapter2.MyViewHolder2> {
    private List<Bean.RsBean.ChildrenBeanX> datas;
    private LayoutInflater inflater;
    private Context context;

    public RecyclerViewAdapter2(Context context, List<Bean.RsBean.ChildrenBeanX> datas) {
        inflater = LayoutInflater.from(context);
        this.datas = datas;
        this.context = context;
    }

    //创建每一行的View 用RecyclerView.ViewHolder包装
    @Override
    public RecyclerViewAdapter2.MyViewHolder2 onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = inflater.inflate(R.layout.item_right, null);
        return new MyViewHolder2(itemView);
    }

    //给每一行View填充数据
    @Override
    public void onBindViewHolder(RecyclerViewAdapter2.MyViewHolder2 holder, int position) {


        holder.textview.setText(datas.get(position).dirName);


        final List<Bean.RsBean.ChildrenBeanX.ChildrenBean> children = datas.get(position).children;

        holder.grd.setAdapter(new Mybs(context, children));

        holder.grd.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String name = children.get(position).dirName;
                Toast.makeText(context, name, Toast.LENGTH_SHORT).show();
            }
        });
    }

    //数据源的数量
    @Override
    public int getItemCount() {
        return datas.size();
    }

    class MyViewHolder2 extends RecyclerView.ViewHolder {
        private TextView textview;
        private GridView grd;

        public MyViewHolder2(View itemView) {
            super(itemView);
            textview = (TextView) itemView.findViewById(R.id.type2);
            grd = (GridView) itemView.findViewById(R.id.grd);


//            ListAdapter adapter = grd.getAdapter();
//
//            if(adapter==null){
//                return;
//            }
//
//            int totalHeight = 0;
//            int lineNum = 0;
//
//            View item = adapter.getView(0, null, grd);
//            item.measure(0,0);
//
//            totalHeight = item.getMeasuredHeight()*lineNum;
//
//            ViewGroup.LayoutParams params = grd.getLayoutParams();
//
//            params.height = totalHeight;
//
//            ((ViewGroup.MarginLayoutParams)params).setMargins(10,10,10,10);
//
//            grd.setLayoutParams(params);
        }
    }


}
